package com.yiqi.lottery.feature.documentary.view;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.AppUtil;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.lottery.R;
import com.yiqi.lottery.entity.bo.HallUserBo;
import com.yiqi.lottery.entity.bo.OrderDetailBo;
import com.yiqi.lottery.entity.bo.PersonalLotteryBo;
import com.yiqi.lottery.feature.documentary.contract.PersonContract;
import com.yiqi.lottery.feature.documentary.dependencies.home.DaggerFollowComponent;
import com.yiqi.lottery.feature.documentary.dependencies.home.FollowPresenterModule;
import com.yiqi.lottery.feature.documentary.view.adapter.LotteryDetailActivity;
import com.yiqi.lottery.feature.documentary.view.adapter.PersonalLotteryAdapter;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.List;

public class PersonalInfoActivity extends BaseActivity implements BaseQuickAdapter.OnItemChildClickListener
        , PersonContract.View, CompoundButton.OnCheckedChangeListener, BaseQuickAdapter.OnItemClickListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_documentary_person_focus)
    CheckBox mPersonFocus;
    @BindView(R.id.iv_documentary_person_head)
    ImageView mIvDocumentaryPersonHead;
    @BindView(R.id.tv_documentary_person_name)
    TextView mTvDocumentaryPersonName;
    @BindView(R.id.rv_personal)
    RecyclerView mRvPersonal;
    @BindView(R.id.tv_personal_record)
    TextView mTvPersonalRecord;
    @BindView(R.id.tv_personal_jiangjin)
    TextView mTvPersonalJiangjin;
    @BindView(R.id.tv_personal_fans)
    TextView mTvPersonalFans;
    @BindView(R.id.tv_personal_record_7)
    TextView mTvPersonalRecord7;
    @BindView(R.id.tv_personal_jiangjin_7)
    TextView mTvPersonalJiangjin7;
    @BindView(R.id.tv_personal_daihong_7)
    TextView mTvPersonalDaihong7;
    private PersonalLotteryAdapter mLianhongAdapter;

    private HallUserBo mData;

    @Inject

    PersonContract.Presenter mPresenter;

    private Integer id;
    private EmptyView mEmptyView;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_personal_info;
    }

    @Override
    protected void initInjector() {
        DaggerFollowComponent.builder().followPresenterModule(new FollowPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initSystemParams() {
//        super.initSystemParams();
        AppUtil.fullScreen(this);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "个人信息", true, true);
        initLianhongAdapter();
    }

    @Override
    protected void initData() {

        id = this.getIntent().getIntExtra("id", 0);
        mPresenter.getUserInfo(id);

    }

    @Override
    protected void initEvent() {
        mPersonFocus.setOnCheckedChangeListener(this);
    }

    @OnClick(R.id.tv_documentary_person_focus)
    public void onViewClicked() {
    }


    private void initLianhongAdapter() {
        mLianhongAdapter = new PersonalLotteryAdapter();
        mEmptyView = new EmptyView(mRvPersonal);
        mRvPersonal.setLayoutManager(new LinearLayoutManager(this));
        mRvPersonal.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(25));
        mLianhongAdapter.bindToRecyclerView(mRvPersonal);
        mLianhongAdapter.setOnItemChildClickListener(this);
        mLianhongAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(this, LotteryDetailActivity.class);
        intent.putExtra("id", mData.getList().get(position).getOrder_id());
        intent.putExtra("user_id", id);
        startActivity(intent);
    }

    @Override
    public void showFollowSuccess(String id) {
//        showMsg(id);
    }

    @Override
    public void showFollowError(String ex) {
        showErrorMsg(ex);
    }

    @Override
    public void showFollowOrderSuccess(String msg) {

    }

    @Override
    public void showUserInfoSuccess(HallUserBo info) {
        mData = info;
        AM.image().bindToCircleObject(info.getAvatar(), mIvDocumentaryPersonHead);
        mTvDocumentaryPersonName.setText(info.getUsername());
        mTvPersonalRecord.setText(info.getRecord_amount());
        mTvPersonalJiangjin.setText("奖金" + info.getIncome_amount());
        mTvPersonalFans.setText("砖粉" + info.getFriend_num());
        mTvPersonalRecord7.setText(info.getSeven_record_amount());
        mTvPersonalJiangjin7.setText(info.getSeven_income_amount() + "");
        mTvPersonalDaihong7.setText(info.getSeven_follow_num() + "");
        if (info.getIs_friend() == 1) {
            mPersonFocus.setChecked(true);
            mPersonFocus.setText("已关注");
        } else {
            mPersonFocus.setChecked(false);
            mPersonFocus.setText("+关注");
        }
        if (info.getList().size() > 0) {
            mLianhongAdapter.setNewData(info.getList());
        } else {
            mLianhongAdapter.setEmptyView(mEmptyView.getEmptyView());
        }

    }

    @Override
    public void showUserInfoError(String ex) {
        T.showShort("showUseError" + ex);
    }

    @Override
    public void showGendanDetailSuccess(OrderDetailBo hallUserBo) {

    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (isChecked) {
            if (mData != null)
                mPresenter.followUser(mData.getUser_id());
            mPersonFocus.setText("已关注");
        } else {
            if (mData != null)
                mPresenter.unfollowUser(mData.getUser_id());
            mPersonFocus.setText("+关注");
        }

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(this, LotteryDetailActivity.class);
        intent.putExtra("id", mData.getList().get(position).getOrder_id());
        intent.putExtra("user_id", id);
        startActivity(intent);
    }
}
